package org.example.completed.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.completed.domain.ActivityReservation;
import org.example.completed.domain.Reservation;
import org.example.completed.domain.User;
import org.example.completed.mapper.ReservationMapper;
import org.example.completed.mapper.UserMapper;
import org.example.completed.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    public ReservationServiceImpl(ReservationMapper reservationMapper) {
        this.reservationMapper = reservationMapper;
    }

    @Override
    public boolean register(String userId, String contact, LocalDateTime visitTime) {
        // 检查该 userId 是否已经存在预约记录
        List<Reservation> existingReservations = reservationMapper.selectByUserId(userId);
        if (!existingReservations.isEmpty()) {
            // 如果已经存在预约记录，返回 false
            return false;
        }

        Reservation originalReservation = reservationMapper.selectById("1");

        // 创建新的预约记录
        Reservation newReservation = new Reservation();

        User user = userMapper.selectById(userId);
        if (user != null) {
            newReservation.setUserAcoount(user.getUserAcoount());
        }else {
            return false;
        }

        // 复制原始记录的内容
        newReservation.setName(originalReservation.getName());
        newReservation.setVisitTime(originalReservation.getVisitTime());
        // 填入传入的 userId 和 contact
        newReservation.setUserId(userId);
        newReservation.setContact(contact);
        newReservation.setVisitTime(visitTime);

        // 插入新记录
        return reservationMapper.insert(newReservation) > 0;
    }

}